// 重庆分店的房间的路由
const express=require('express')
const pool=require('../pool.js')
const router=express.Router()

// 查询所有重庆分店的房间
router.get('/list',(req,res,next)=>{
    pool.query('select * from ChongqingBranch',(err,r)=>{
        if(err){
            return next(err)
        }
        res.send({code:200,msg:'查询重庆分店的房间成功',data:r})
    })
})

// 通过deptId查询重庆分店的房间
router.get('/list/:roomId',(req,res,next)=>{
    let obj=req.params
    pool.query('select * from ChongqingBranch where roomId= ?',[obj.roomId],(err,r)=>{
        if(err){
            return next(err)
        }
        res.send({code:200,msg:'查询指定重庆分店的房间成功',data:r})
    })
})

//增加重庆分店的房间
router.post('/add',(req,res,next)=>{
    let obj=req.body
    pool.query('insert into ChongqingBranch set ?',[obj],(err,r)=>{
        if(err){
            return next(err)
        }
        res.send({code:200,msg:'增加重庆分店的房间成功',data:r})
    })
})

//删除重庆分店的房间
router.get('/del',(req,res,next)=>{
    let obj=req.query
    pool.query('delete from ChongqingBranch where roomId=?',[obj.roomId],(err,r)=>{
        if(err){
            return next(err)
        }
        res.send({code:200,msg:'删除重庆分店的房间成功',data:r})
    })
})

//修改重庆分店的房间信息
router.post('/update',(req,res,next)=>{
    let obj=req.body
    pool.query('update ChongqingBranch set ? where roomId=?',[obj,obj.roomId],(err,r)=>{
        if(err){
            return next(err)
        }
        res.send({code:200,msg:'修改重庆分店的房间成功',data:r})
    })
})

//分页查询
router.get('/pageList',(req,res,next)=>{
    let obj=req.query
    let pno=obj.page  //当前页
    let pcount=obj.pagesize //每页显示几条
    let start = (pno-1)*pcount
    let end = Number.parseInt(pcount)
    let pagecount=0 //总共有几页
    pool.query('select * from ChongqingBranch limit ? , ?;select count(*) sum from ChongqingBranch',[start,end],(err,r)=>{
        if(err){
            return next(err)
        }
        pagecount = Math.ceil(r[1][0].sum / pcount)
        res.send({code:200,msg:'重庆分页查询成功',data:r,pagecount,pagesize:pcount,page:pno})

    })
})

// 查询房间售出情况
router.get('/roomSale',(req,res,next)=>{
    pool.query('select count(*) yes1 from ChongqingBranch where isUsed="是";select count(*) no1 from ChongqingBranch where isUsed="否";',(err,r)=>{
        if(err){
            return next(err)
        }
        let yes1=r[0][0].yes1
        let no1=r[1][0].no1
        res.send({code:200,msg:'房间售出情况查询成功',data:r,yes1,no1})
    })
})

module.exports=router